package com.itboye.ihomebank.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.OpenIdBean;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.RegisterBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.util.controller.LoginController;
import com.itboye.ihomebank.util.controller.LoginedState;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.SavePersonData;

import java.util.Observable;
import java.util.Observer;

/**
 * 注册第3步（设置密码）
 * 
 */
public class RegisterActivityThree extends BaseOtherActivity implements
		OnClickListener, Observer {
	View v_statusbar;
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题

	private EditText res_three_num;// 密码
	private TextView res_three_ok;// 完成并登陆
	private Intent intent;
	ToggleButton res_three_tb;

	private String phone;// 接上页面传来的手机号
	private String yzm;// 接上页面传来的验证码

	UserPresenter userPresenter;
	KeyPresenter keyPresenter;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();

			break;
		case R.id.res_three_ok:
			UserRegister();
			break;
		case R.id.res_three_tb:// 密码可见
			if (res_three_tb.isChecked()) {
				res_three_num
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				res_three_num.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);

			}
			break;

		default:
			break;
		}

	}

	/**
	 * 注册
	 */
	private void UserRegister() {

		if (TextUtils.isEmpty(res_three_num.getText().toString())) {
			ByAlert.alert("请输入密码");
			return;
		}
		userPresenter = new UserPresenter(this);
		userPresenter.register("3", "0", yzm, phone, res_three_num.getText()
				.toString());

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == UserPresenter.register_success) {
					RegisterBean bean = (RegisterBean) entity.getData();
					Log.i("result", bean.toString());
					if (bean != null) {
						SPUtils.put(RegisterActivityThree.this, null,
								SPContants.IS_LOGINED, true);
						login();
						RegisterActivityThree.this.finish();
						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());

						ByAlert.alert("注册成功");

					}
				}
				if (entity.getEventType() == UserPresenter.register_fail) {
					ByAlert.alert("注册失败");
				}
				if (entity.getEventType() == UserPresenter.user_success) {
					PersonDataBean bean = (PersonDataBean) entity.getData();
					if (bean != null) {
						SavePersonData.saveData(RegisterActivityThree.this,bean);

						userPresenter.bindKeJiXia(bean.getUid(),true);//直接注册科技侠
//						Intent intent1 = new Intent(ActivitySetting.EXITCHANGE);
//						sendBroadcast(intent1);
						userPresenter.getOpenId(bean.getUid());//获取科技侠openid
						// 发送广播通知更改头像
						Intent intent2 = new Intent("changeavart");
						sendBroadcast(intent2);
					}
				} else if (entity.getEventType() == UserPresenter.bindKeJiXia_success) {
					//ByAlert.alert(entity.getData());
				} else if (entity.getEventType() == UserPresenter.bindKeJiXia_fail) {
					//ByAlert.alert(entity.getData());
				}else if (entity.getEventType() == UserPresenter.openid_success) {
					//ByAlert.alert(entity.getData());
					OpenIdBean b= (OpenIdBean) entity.getData();
					SPUtils.put(this, null,SPContants.OPENID, b.getOpenid());
				} else if (entity.getEventType() == UserPresenter.openid_fail) {
					//ByAlert.alert(entity.getData());
				}
			}
		}

	}

	private void bingKeJiXia() {
		// TODO Auto-generated method stub
		String uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		String name=SPUtils.get(this, null, SPContants.USERNAME, "") + "";
		String pass=SPUtils.get(this, null, SPContants.PASSWORD, "") + "";
		Log.v("user",uid+"::"+name+"::"+pass+"");
		userPresenter.bindKeJiXia(uid,true);
	}

	/**
	 * 登录
	 */
	public void login() {
		keyPresenter=new KeyPresenter(this);
		userPresenter = new UserPresenter(this);
		userPresenter.login(phone, res_three_num.getText().toString());
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_register_three;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("设置密码");

		setStatusBarHeight(v_statusbar);
		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm = intent.getStringExtra("yzm");
		
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}
}
